/*
 * DummyUserResource.cpp
 *
 *  Created on: 2013. 10. 22.
 *      Author: mega@gamevil.com
 */

#include "../User.h"
#include "../NLogic/Bag.h"
#include "../NLogic/Equip.h"
#include "../NLogic/Item.h"
#include "../NLogic/Static.h"
#include "DummyUserResource.h"


namespace ProjectZ { namespace NResource {

DummyUserResource::DummyUserResource()
{

}

DummyUserResource::~DummyUserResource()
{
	std::for_each(clsDummyUserTree.begin(), clsDummyUserTree.end(), [](DUMMYUSERTREE::value_type& v) {
		::ProjectZ::User::Factory::Instance().DEL(v.second);
	} );
	clsDummyUserTree.clear();
}

DummyUserResource::DUMMYUSERTREE &
DummyUserResource::GetDummyUserTree()
{
	return clsDummyUserTree;
}

User *
DummyUserResource::GetDummyUser(int userseq)
{
	auto iter = clsDummyUserTree.find(userseq);
	if (iter == clsDummyUserTree.end())
		return NULL;

	return iter->second;
}

bool
DummyUserResource::LoadResource()
{
	if (false == this->LoadUserInfo())
		return false;

	if (false == this-LoadCharacterInfo())
		return false;

	if (false == this->LoadItemInfo(0))
		return false;

	if (false == this->LoadItemInfo(1))
		return false;

	return true;
}

bool
DummyUserResource::LoadUserInfo()
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("select * from resource_dummy_userinfo");

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	while( clsAuto->Next() )
	{
		User * pDummy = ::ProjectZ::User::Factory::Instance().NEW();
		pDummy->Initialize();

		int seq;
		int zen;
		Toolkit::Str<20> regdate;
		Toolkit::Str<80> nickname;
		Toolkit::Str<80> uuid;
		Toolkit::Str<1024> profileurl;

		clsAuto->GetRecord() >> seq;
		clsAuto->GetRecord() >> pDummy->GetUserInfo().userseq;

		for (int i = 0; i < 8; i++)
		{
			slot clsSlot;
			clsAuto->GetRecord() >> clsSlot.open;
			clsAuto->GetRecord() >> clsSlot.character_seq;
			clsAuto->GetRecord() >> clsSlot.makeCharacter;
			clsAuto->GetRecord() >> clsSlot.remainStatResetCount;
			clsAuto->GetRecord() >> clsSlot.classtype;
			clsAuto->GetRecord() >> clsSlot.level;
			clsAuto->GetRecord() >> regdate;
			//clsSlot.reg_date = regdate.c_str();

			pDummy->GetUserInfo().array_Slot[i] = clsSlot;
		}

		//clsAuto->GetRecord() >> pDummy->GetUserInfo().zen;
		clsAuto->GetRecord() >> zen;
		clsAuto->GetRecord() >> pDummy->GetUserInfo().main_slot_index;
		clsAuto->GetRecord() >> nickname;
		pDummy->GetUserInfo().nickname = nickname.c_str();

		clsAuto->GetRecord() >> profileurl;
		pDummy->GetUserInfo().profile_url = profileurl.c_str();

		clsAuto->GetRecord() >> pDummy->GetUserInfo().score;
		clsAuto->GetRecord() >> pDummy->GetUserInfo().heart_blocked;
		clsAuto->GetRecord() >> pDummy->GetUserInfo().invite_blocked;
		clsAuto->GetRecord() >> pDummy->GetUserInfo().profile_opened;
		clsAuto->GetRecord() >> pDummy->GetUserInfo().company;
		clsAuto->GetRecord() >> pDummy->GetUserInfo().sale_code;
		clsAuto->GetRecord() >> pDummy->GetUserInfo().u8intro_state;
		clsAuto->GetRecord() >> pDummy->GetUserInfo().wp_last_week;
		clsAuto->GetRecord() >> pDummy->GetUserInfo().wp_cur_week;
		clsAuto->GetRecord() >> pDummy->GetUserInfo().wp_reg_date;

		for (int i = 0; i < 4; i++)
		{
			clsAuto->GetRecord() >> pDummy->GetUserInfo().array_tutorial[i];
		}

		clsAuto->GetRecord() >> uuid;
		pDummy->GetUserInfo().uuid = uuid.c_str();

		pDummy->SetUserSeq(pDummy->GetUserInfo().userseq);

		clsDummyUserTree[pDummy->GetUserInfo().userseq] = pDummy;
	}

	return true;
}


bool
DummyUserResource::LoadCharacterInfo()
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("select * from resource_dummy_characterinfo");

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	while( clsAuto->Next() )
	{
		Toolkit::Str<30>	wincomment;
		Toolkit::Str<500>	dungeon_clear_info;
		Toolkit::Str<10>	bag_order_info;
		Toolkit::Str<500>	buff_info;

		CharacterInfo		_characterInfo;

		clsAuto->GetRecord() >> _characterInfo.characterseq;
		clsAuto->GetRecord() >> _characterInfo.userSeq;

		clsAuto->GetRecord() >> _characterInfo.slotindex;
		clsAuto->GetRecord() >> _characterInfo.classtype;
		clsAuto->GetRecord() >> _characterInfo.remain_stat_point;
		clsAuto->GetRecord() >> _characterInfo.remain_skill_point;
		clsAuto->GetRecord() >> _characterInfo.ap_current;
		clsAuto->GetRecord() >> _characterInfo.ep_current;
		clsAuto->GetRecord() >> _characterInfo.ep_max;

		clsAuto->GetRecord() >> _characterInfo.weapon;
		clsAuto->GetRecord() >> _characterInfo.weapon_iconidx;
		clsAuto->GetRecord() >> _characterInfo.helmet;
		clsAuto->GetRecord() >> _characterInfo.helmet_iconidx;
		clsAuto->GetRecord() >> _characterInfo.armor;
		clsAuto->GetRecord() >> _characterInfo.armor_iconidx;
		clsAuto->GetRecord() >> _characterInfo.glove;
		clsAuto->GetRecord() >> _characterInfo.shoes;
		clsAuto->GetRecord() >> _characterInfo.necklace;
		clsAuto->GetRecord() >> _characterInfo.cloak;
		clsAuto->GetRecord() >> _characterInfo.ring;
		clsAuto->GetRecord() >> _characterInfo.charm;
		clsAuto->GetRecord() >> _characterInfo.avartar;
		clsAuto->GetRecord() >> _characterInfo.avartar_iconidx;
		clsAuto->GetRecord() >> _characterInfo.vehicle;
		clsAuto->GetRecord() >> _characterInfo.vehicle_iconidx;
		clsAuto->GetRecord() >> _characterInfo.title;
		clsAuto->GetRecord() >> _characterInfo.fairy;
		clsAuto->GetRecord() >> _characterInfo.battlepet;

		clsAuto->GetRecord() >> _characterInfo.exp;

		clsAuto->GetRecord() >> _characterInfo.level;
		clsAuto->GetRecord() >> _characterInfo.str;
		clsAuto->GetRecord() >> _characterInfo.dex;
		clsAuto->GetRecord() >> _characterInfo.con;
		clsAuto->GetRecord() >> _characterInfo.spi;


		for (int i = 0; i < 8; i++)
		{
			QuickSlot clsQuickSlot;
			clsAuto->GetRecord() >> clsQuickSlot.type;
			clsAuto->GetRecord() >> clsQuickSlot.index;

			_characterInfo.array_QuickSlot[i] = clsQuickSlot;
		}

		for (int i = 0; i < 20; i++)
		{
			clsAuto->GetRecord() >> _characterInfo.array_Skill[i];
		}

		clsAuto->GetRecord() >> _characterInfo.gold;

		clsAuto->GetRecord() >> _characterInfo.event_stamina;
		clsAuto->GetRecord() >> _characterInfo.recover_stamina_sec;


		clsAuto->GetRecord() >> _characterInfo.drill_status;
		clsAuto->GetRecord() >> _characterInfo.drill_time;
		clsAuto->GetRecord() >> _characterInfo.drill_start;
		clsAuto->GetRecord() >> _characterInfo.drill_end;

		clsAuto->GetRecord() >> _characterInfo.consecutive_win;
		clsAuto->GetRecord() >> wincomment;
		_characterInfo.win_comment = wincomment.c_str();

		clsAuto->GetRecord() >> _characterInfo.reg_date;

		clsAuto->GetRecord() >> dungeon_clear_info;
		_characterInfo.dungeon_clear_info = dungeon_clear_info.c_str();

		clsAuto->GetRecord() >> bag_order_info;
		_characterInfo.bag_order_info = bag_order_info.c_str();

		clsAuto->GetRecord() >> buff_info;
		_characterInfo.buff_info = buff_info.c_str();

		User * pDummy = this->GetDummyUser(_characterInfo.userSeq);
		if (NULL != pDummy)
		{
			pDummy->SetCharacterInfo(_characterInfo, _characterInfo.slotindex);
		}
	}

	return true;
}

bool
DummyUserResource::LoadItemInfo(int mod)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("select * from resource_dummy_iteminfo_%d", mod);

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	while( clsAuto->Next() )
	{
		uint64_t itemseq;
		int userseq;
		int characterseq;
		int status;

		clsAuto->GetRecord() >> itemseq >> userseq >> characterseq >> status;

		User * pDummy = this->GetDummyUser(userseq);
		if (NULL == pDummy)
			continue;

		NLogic::Item::Data clsData;

		clsAuto->GetRecord() >> clsData.bag_type;
		clsAuto->GetRecord() >> clsData.bag_slot_number;
		clsAuto->GetRecord() >> clsData.quantity;
		clsAuto->GetRecord() >> clsData.class_type;
		clsAuto->GetRecord() >> clsData.sub_type;
		clsAuto->GetRecord() >> clsData.tid;
		clsAuto->GetRecord() >> clsData.iconidx;
		clsAuto->GetRecord() >> clsData.item_type;
		clsAuto->GetRecord() >> clsData.cur_duration;
		clsAuto->GetRecord() >> clsData.max_duration;
		clsAuto->GetRecord() >> clsData.set_type;
		clsAuto->GetRecord() >> clsData.non_identity;
		clsAuto->GetRecord() >> clsData.cur_refine_step;
		clsAuto->GetRecord() >> clsData.quality;
		clsAuto->GetRecord() >> clsData.level;

		for (int i = 0; i < 7; ++i)
		{
			clsAuto->GetRecord() >> clsData.eff_type[i];
			clsAuto->GetRecord() >> clsData.eff_pos[i];
			clsAuto->GetRecord() >> clsData.eff_value[i];
		}

		clsAuto->GetRecord() >> clsData.open_upgrade_stone_slot;
		clsAuto->GetRecord() >> clsData.ability_enhance_rate;
		clsAuto->GetRecord() >> clsData.max_enhance_step;
		clsAuto->GetRecord() >> clsData.buy_use;
		clsAuto->GetRecord() >> clsData.evolve_step;
		clsAuto->GetRecord() >> clsData.evolve_max;
		clsAuto->GetRecord() >> clsData.evolve_point;
		clsAuto->GetRecord() >> clsData.evolve_percent;
		clsAuto->GetRecord() >> clsData.evolve_value;
		clsAuto->GetRecord() >> clsData.reg_date;

		NLogic::Bag * pBag = pDummy->GetBag(static_cast<INVEN_BAG_TYPE>(clsData.bag_type));
		if (NULL == pBag)
			continue;

		if (true == pBag->IsBagFull())
			continue;

		NLogic::Item * pItem = NLogic::Static::InstancePtr()->NEW_ITEM();
		pItem->Open_FromDatabase(itemseq, clsData, pDummy);
		pBag->AddItem(pItem);

		NLogic::Equip * pEquip = pDummy->GetEquip();
		pEquip->EquipAddFromCharacterInfo( 0 );
		pEquip->UpdateCharacterInfo();
	}

	return true;
}

} /*NResource*/ } /* namespace ProjectZ */
